package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.SecurityUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctDetectionRulesBo;
import com.rzdata.poct.system.domain.business.po.PoctDetectionRules;
import com.rzdata.poct.system.domain.business.vo.PoctDetectionRulesVo;
import com.rzdata.poct.system.mapper.PoctDetectionRulesMapper;
import com.rzdata.poct.system.service.IPoctDetectionRulesService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * POCT检测规则Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctDetectionRulesServiceImpl extends ServicePlusImpl<PoctDetectionRulesMapper, PoctDetectionRules, PoctDetectionRulesVo> implements IPoctDetectionRulesService {

    @Override
    public PoctDetectionRulesVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public PoctDetectionRulesVo queryByItemId(Long id) {
        return this.baseMapper.queryByItemId(id);
    }

    @Override
    public TableDataInfo<PoctDetectionRulesVo> queryPageList(PoctDetectionRulesBo bo) {
        PagePlus<PoctDetectionRules, PoctDetectionRulesVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctDetectionRulesVo> queryList(PoctDetectionRulesBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctDetectionRules> buildQueryWrapper(PoctDetectionRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctDetectionRules> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getItemId() != null, PoctDetectionRules::getItemId, bo.getItemId());
        lqw.eq(bo.getProjectId() != null, PoctDetectionRules::getProjectId, bo.getProjectId());
        lqw.eq(bo.getDeptId() != null, PoctDetectionRules::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), PoctDetectionRules::getItemName, bo.getItemName());
        lqw.like(StringUtils.isNotBlank(bo.getItemEname()), PoctDetectionRules::getItemEname, bo.getItemEname());
        lqw.eq(StringUtils.isNotBlank(bo.getMinnormalValue()), PoctDetectionRules::getMinnormalValue, bo.getMinnormalValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxnormalValue()), PoctDetectionRules::getMaxnormalValue, bo.getMaxnormalValue());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), PoctDetectionRules::getUnit, bo.getUnit());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), PoctDetectionRules::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), PoctDetectionRules::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getMinageValue()), PoctDetectionRules::getMinageValue, bo.getMinageValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxageValue()), PoctDetectionRules::getMaxageValue, bo.getMaxageValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMincriticalValue()), PoctDetectionRules::getMincriticalValue, bo.getMincriticalValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxcriticalValue()), PoctDetectionRules::getMaxcriticalValue, bo.getMaxcriticalValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMincriticalReference()), PoctDetectionRules::getMincriticalReference, bo.getMincriticalReference());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxcriticalReference()), PoctDetectionRules::getMaxcriticalReference, bo.getMaxcriticalReference());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctDetectionRulesBo bo) {
        PoctDetectionRules add = BeanUtil.toBean(bo, PoctDetectionRules.class);
        add.setCreateBy(SecurityUtils.getUsername());
        add.setCreateTime(new Date());
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctDetectionRulesBo bo) {
        PoctDetectionRules update = BeanUtil.toBean(bo, PoctDetectionRules.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctDetectionRules entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
